package anonymouschat.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import anonymouschat.messages.Message;
import anonymouschat.service.Server;

public class Print {

	/**
	 * Only print debug messages if debugging is turned on
	 */
	private static final boolean PRINT_DEBUG_MESSAGES = Constants.USE_DEBUG_MODE;

	private static final String DATE_FORMAT_NOW = "HH:mm:ss";

	// used to format the string
	private static Calendar cal = null;

	/**
	 * 
	 * @return String nice time format
	 */
	private static String now() {
		cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		return sdf.format(cal.getTime());
	}

	/**
	 * Prints the message"\n", even if debugging is turned off
	 * 
	 */
	public static void info(String msg) {
		System.out.println(msg);
	}

	/**
	 * Prints "&ltSOURCE&gt sending: &ltMESSAGE&gt at &ltNOW()&gt"
	 * 
	 * @param source
	 *            who created the message
	 * @param message
	 *            the message in plain text
	 */
	public static void sending(String source, String message) {
		if (source == null)
			source = "NULL";
		if (message == null)
			message = "NULL";
		System.out.println(source + " SENDING: " + message + " at " + now());

	}

	/**
	 * Prints "&ltDEST&gt received: &ltMESSAGE&gt at &ltNOW()&gt"
	 * 
	 * @param dest
	 *            who received the message
	 * @param message
	 *            the message in plain text
	 */
	public static void received(String dest, String message) {
		if (dest == null)
			dest = "NULL";
		if (message == null)
			message = "NULL";
		System.out.println(dest + " RECEIVED: " + message + " at " + now());
	}

	/**
	 * Prints "Debug: &ltDEBUGMESSAGE&gt"
	 * 
	 * @param debugMessage
	 *            output only if debug is set to true
	 */
	public static void debug(String debugMessage) {
		if (debugMessage == null)
			debugMessage = "NULL";
		if (PRINT_DEBUG_MESSAGES) {
			System.out.println(now() + " Debug: " + debugMessage);
		}
	}

	/**
	 * Prints "&ltERRORMESSAGE&gt"`
	 * 
	 * @param errorMessage
	 *            the error message to be displayed
	 */
	public static void error(String errorMessage, Server srv) {
		if (errorMessage == null)
			errorMessage = "NULL";
		System.err.println(errorMessage);
		if (srv != null) {
			Server.addAlert(now() + ": " + errorMessage);
		}
	}

	/**
	 * 
	 * Prints "&ltERRORMESSAGE&gt with &lte.STACKTRACE&gt"
	 * 
	 * @param errorMessage
	 *            the message to display
	 * @param e
	 *            the exception to print the stack trace of
	 * @param srv
	 *            a handle to the server for updating the GUI
	 */
	public static void error(String errorMessage, Exception e, Server srv) {
		Print.error(errorMessage, srv);
		if (e == null)
			e = new Exception("NULL");
		e.printStackTrace();
	}

	/**
	 * Prints a block that displays any printable information in a message
	 * 
	 * @param msg
	 *            the message to be printed
	 */
	public static void debug(Message msg) {
		if (msg != null && PRINT_DEBUG_MESSAGES) {
			System.out.print("*****MESSAGE*****\n" + msg.getAllContents()
					+ "\n*****END MESSAGE*****\n");
		}
	}

	public static void error(String string) {
		Print.error(string, (Server) null);
	}

	public static void error(String string, Exception e) {
		Print.error(string, e, null);
	}

}
